//
//  CLChatTableViewCell.m
//  MasonryTest
//
//  Created by Charles on 2016/12/6.
//  Copyright © 2016年 Charles. All rights reserved.
//

#import "CLChatTableViewCell.h"

@implementation CLChatTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self makeView];
    }
    return self;
}

- (void)makeView {
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.contentView.backgroundColor = [UIColor lightGrayColor];
    self.headImage.layer.cornerRadius = 25;
    self.headImage.clipsToBounds = YES;
    self.headImage.image = [UIImage imageNamed:@"meinv2.jpg"];
    
}

- (void)setContent:(NSString *)content direction:(BOOL)direction {

    UIView * superView = self.contentView;

    if (!direction) {
        [self.headImage makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.equalTo(10);
            make.width.height.equalTo(50);
        }];
        
        [self.bubbleImage makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(superView).offset(70);
            make.top.equalTo(superView).offset(10);
            make.bottom.equalTo(superView.bottom).offset(-10);
            make.width.lessThanOrEqualTo(200);
            make.height.lessThanOrEqualTo(50);
        }];
        UIImage *bubble = [UIImage imageNamed:@"receive_bubble"];
        self.bubbleImage.image = [bubble stretchableImageWithLeftCapWidth:20 topCapHeight:29];
       
        [self.textContent makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.bubbleImage.left).offset(10);
            make.top.equalTo(self.bubbleImage.top).offset(5);
            make.bottom.equalTo(self.bubbleImage.bottom).offset(-5);
            make.width.lessThanOrEqualTo(200);
        }];
    }else {
        [self.headImage makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(10);
            make.right.equalTo(-10);
            make.width.height.equalTo(50);
        }];
        
        [self.bubbleImage makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(superView).offset(-70);
            make.top.equalTo(superView).offset(10);
            make.bottom.equalTo(superView.bottom).offset(-10);
            make.width.lessThanOrEqualTo(200);
            make.height.lessThanOrEqualTo(50);
        }];
        UIImage *bubble = [UIImage imageNamed:@"send_bubble"];
        self.bubbleImage.image = [bubble stretchableImageWithLeftCapWidth:20 topCapHeight:29];
       
        [self.textContent makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.bubbleImage.right).offset(-10);
            make.top.equalTo(self.bubbleImage.top).offset(5);
            make.bottom.equalTo(self.bubbleImage.bottom).offset(-5);
            make.width.lessThanOrEqualTo(200);
        }];
    }
    self.textContent.text = content;
    [self.textContent sizeToFit];

    if (!direction) {
        [self.bubbleImage updateConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(self.textContent.width).offset(15);
            make.height.equalTo(self.textContent.height).offset(10);
        }];

    }else {
        [self.bubbleImage updateConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(self.textContent.width).offset(15);
            make.height.equalTo(self.textContent.height).offset(10);
        }];
    }
}

+ (CGFloat)rowHeightForContent:(NSString *)content {
    
    
    CGSize titleSize = [content boundingRectWithSize:CGSizeMake(kCellContentMaxWidth - 20, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil].size;
    CGFloat height = titleSize.height + 20;

    return height;
}

- (UIImageView *)bubbleImage {
    if (nil == _bubbleImage) {
        _bubbleImage = [[UIImageView alloc]init];
        _bubbleImage.backgroundColor = [UIColor lightGrayColor];
        [self.contentView addSubview:_bubbleImage];
    }
    return _bubbleImage;
}

- (UIImageView *)headImage {
    if (nil == _headImage) {
        _headImage = [[UIImageView alloc]init];
        _headImage.backgroundColor = [UIColor lightGrayColor];
        [self.contentView addSubview:_headImage];
    }
    return _headImage;
}

- (UILabel *)textContent {
    if (nil == _textContent) {
        _textContent = [[UILabel alloc]init];
        _textContent.numberOfLines = 0;
        _textContent.font = [UIFont systemFontOfSize:14.0f];
        [self.contentView addSubview:_textContent];
    }
    return _textContent;
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
